package com.wang;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;
//String常用方法
public class StringMethod {
    public static void main(String[] args) throws UnsupportedEncodingException {
        byte [] bytes = {66,67,68,69,70,71,72};
        String s2 = new String(bytes);
        System.out.println(s2);
        s2 = new String(bytes,0,5);
        System.out.println(s2);
        char [] chars = {'A','B','C','D','E','F'};
        String s3 = new String(chars);
        System.out.println(s3);
        s3 = new String(chars,2,4);
        System.out.println(s3);

        String s = "qwerasd";
        System.out.println(s.length());//s长度
        System.out.println(s.charAt(1));//s下标为1的字符
        //遍历字符串每一个字符
        for (int i = 0; i < s.length(); i++) {
            System.out.print(s.charAt(i)+" ");
        }
        System.out.println();
        // 实现了Comparable接口，重写了compareTo方法，能实现String比较字符串大小，正数1字符串大，负数2字符串大
        System.out.println("http".compareTo("ht"));
        System.out.println("http".compareTo("x"));
        //区分大小写
        System.out.println("http".compareTo("HTTP"));
        //可以实现不区分大小写比较
        System.out.println("http".compareToIgnoreCase("HTTP"));//返回0相等
        //contains 判断是否包含
        System.out.println(s.contains("q"));
        //startsWith 判断结尾是否是该内容
        System.out.println(s.startsWith("sd"));
        //endsWith 判断开头是否是该内容
        System.out.println(s.endsWith("qw"));
        //equals 比较是否相同
        System.out.println(s.equals("qwerasd"));
        //equalsIgnoreCase 比较是否相同(忽略大小写比较)
        System.out.println(s.equalsIgnoreCase("QWEASD"));
        //getBytes（） 返回字符串当前默认编码的字节数组(现在默认的是UTF-8)
        byte [] byte1 = s.getBytes();
        System.out.println(Arrays.toString(byte1));
        //改为GBK格式的对应数组
        byte [] gbks = s.getBytes("GBK");
        //将gbks转为字符串
        System.out.println(Arrays.toString(gbks));
        //把gbks转成默认的编码格式
        String str = new String(gbks);
        str = new String(gbks,"GBK");
        System.out.println(str);
    }

}
